package com.liao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liao.bean.Role;
import com.liao.bean.User;
import com.liao.bean.result.Msg;
import com.liao.bean.result.PageResult;
import com.liao.dao.RoleDao;
import com.liao.dao.UserDao;
import com.liao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<User> selectUserList() throws Exception {
        return userDao.selectUserList();
    }

    @Override
    public User login(User inputUser) throws Exception {
        return userDao.queryUserByAcount(inputUser);
    }

    @Override
    public PageResult<User> queryRoleListByPage(Integer currentPage, Integer pageSize, String userName) throws Exception {
        PageHelper.startPage(currentPage,pageSize);
        List<User> userList = userDao.selectUserListLikeUserName(userName);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return new PageResult<>(0, Msg.USER_QUERY_SUCCESS,pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public void addUser(User user) throws Exception {
        userDao.addUser(user);
    }

    @Override
    public void editUser(String oldUserId, String userId, String userName, String account, String password) throws Exception {
        HashMap<String,Object> map = new HashMap<>();
        map.put("oldUserId",oldUserId);
        map.put("userId",userId);
        map.put("userName",userName);
        map.put("account",account);
        map.put("password",password);
        userDao.updateUser(map);
    }

    @Override
    public List<Role> queryUserList() throws Exception {
        return roleDao.selectRoleList();
    }

    @Override
    public void deleteUser(String userId) throws Exception {
        userDao.deleteUserById(userId);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        try {
            //根据账户查询用户信息
            User existUser=userDao.selectUserByAccount(username);
            //根据账户（user_info）查询用户角色（sys_role）
            List<Role> roleList = roleDao.selectRoleByAccount(username);
            List<SimpleGrantedAuthority> authorities = new ArrayList<>();
            for (Role role : roleList) {
                authorities.add(new SimpleGrantedAuthority("ROLE_"+role.getRoleName()));
            }
            return new org.springframework.security.core.userdetails.User(existUser.getAccount(),existUser.getPassword(),authorities);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
